package classes
{
	import classes.events.MySqlErrorEvent;
	import classes.events.MySqlResultEvent;
	
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;	
	
	public class MySQLService extends EventDispatcher
	{
		private var gateway:NetConnection;
		
		public static var PHP_GATEWAY:String = "php";
		public static var PERL_GATEWAY:String = "perl";
		public static var PYTHON_GATEWAY:String = "python";
		
		
		public function MySQLService( gatewayType:String )
		{
			gateway = new NetConnection();
			gateway.objectEncoding = ObjectEncoding.AMF0;
		 	gateway.connect( "gateway/" + gatewayType +"/gateway.php" );		 	
		}
		
		
		/**
		 * EXECUTE MYSQL QUERY
		 */
		public function execute( resultHandler:Function, faultHandler:Function, query:String ):void
		{
			this.addEventListener( MySqlResultEvent.MYSQL_RESULT, resultHandler );
			this.addEventListener( MySqlErrorEvent.MYSQL_ERROR, faultHandler );
			gateway.call( "MySQLService.execute", new Responder( resp, err ), query );
		} 
		 
		
		/**
		 * GET CHARSET LIST
		 */
		public function getCharsetList( resultHandler:Function ):void
		{
			this.addEventListener( MySqlResultEvent.MYSQL_RESULT, resultHandler );			
			gateway.call( "MySQLService.getCharsets", new Responder( resp, err ) );
		}
		
		
		/**
		 * GET COLLATION LIST
		 */
		public function getCollationList( resultHandler:Function, charset:String = '[default]' ):void
		{
			this.addEventListener( MySqlResultEvent.MYSQL_RESULT, resultHandler );
			
			gateway.call( "MySQLService.getCollations", new Responder( resp, err ), charset );
		}
		
		
		/**
		 * GET DATABASE LIST
		 */
		public function getDatabaseList( resultHandler:Function, faultHandler:Function ):void
		{
			this.addEventListener( MySqlResultEvent.MYSQL_RESULT, resultHandler );
			this.addEventListener( MySqlErrorEvent.MYSQL_ERROR, faultHandler );
			gateway.call( "MySQLService.getDbList", new Responder( resp, err ) );
		}
		
		
		/**
		 * DO AUTHENTICATION
		 */
		public function authenticate( resultHandler:Function, faultHandler:Function, username:String, password:String ):void
		{
			this.addEventListener( MySqlResultEvent.MYSQL_RESULT, resultHandler );
			this.addEventListener( MySqlErrorEvent.MYSQL_ERROR, faultHandler );
			gateway.call( "MySQLService.dbLogin", new Responder( resp, err ), username, password );
		}
		
		
		/**
		 * GENERAL RESULT HANDLER FUNCTION
		 */
		private function resp( o:Object ):void
		{
			if( o.status == MySqlStatus.STATUS_OK )
			{			
				dispatchEvent( new MySqlResultEvent( MySqlResultEvent.MYSQL_RESULT, o ) );
			}
			else if( o.status == MySqlStatus.STATUS_FAULT )			
			{
				dispatchEvent( new MySqlErrorEvent( MySqlErrorEvent.MYSQL_ERROR, o ) );
			}
		}
		
		
		/**
		 * GENERAL ERROR HANDLER
		 */
		private function err( e:Object ):void
		{
			Alert.show( ObjectUtil.toString( e ) );
		}

	}
}